package com.jit.BluetoothKey.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.jit.BluetoothKey.R;
import com.jit.BluetoothKey.activity.base.BaseActivity;
import com.jit.BluetoothKey.utils.SharedPreferencesUtil;
import com.jit.BluetoothKey.utils.UserFiled;

import java.util.ArrayList;
import java.util.List;



public class GuideActivity extends BaseActivity {

    private ViewPager viewPager;
    private List<View> views;
    private List<Integer> data;
    private RelativeLayout relativeLayout;
    private View pointBlack;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
            finish();
            return;
        }
        setContentView(R.layout.activity_guide);

        /**
         *   判断是否第一次登录不是第一次登录直接跳到 广告页面
         */
        if (SharedPreferencesUtil.getIsLogin(this, UserFiled.ISFIRST)){
            Intent intent = new Intent(this, WelcomeActivity.class);
            startActivity(intent);
            finish();
        }
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//去除状态栏

    }

    protected void initListner() {
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                RelativeLayout.LayoutParams params= (RelativeLayout.LayoutParams) pointBlack.getLayoutParams();
                params.leftMargin= (int) ((positionOffset+position)*60);
                pointBlack.setLayoutParams(params);
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    protected void initView() {
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        relativeLayout = (RelativeLayout) findViewById(R.id.relative);
    }

    protected void initData() {
        data = new ArrayList<>();
        data.add(R.mipmap.guide1);
        data.add(R.mipmap.guide2);
        data.add(R.mipmap.guide3);
        views = new ArrayList<>();

        /**
         *   获取最后一个页面进入按钮
         */
        for (int i = 0 ; i <data.size();i++){
            if (i == data.size()-1){  //如果是最后一个欢迎页面 实例化跳转按钮  跳转到登录页面
                View view = View.inflate(this, R.layout.item_guide_lastpager, null);
                ImageView imageView = view.findViewById(R.id.imageView);
                Glide.with(this).load(data.get(i)).asBitmap().into(imageView);
                Button button = view.findViewById(R.id.button);
                button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        SharedPreferencesUtil.setIsLogin(GuideActivity.this,UserFiled.ISFIRST,true);
                        Intent intent = new Intent(GuideActivity.this,LoginActivity.class);
                        startActivity(intent);
                        finish();
                    }
                });
                views.add(view);
            }else {
                views.add(getView(i));
            }
        }
        createPoint();
    }

    private View getView(int i) {
        View view = View.inflate(this, R.layout.item_guide, null);
        ImageView imageView = view.findViewById(R.id.imageView);
        Glide.with(this).load(data.get(i)).centerCrop().into(imageView);
        return view;
    }

    /**
     *   画出引导图滑动跟随移动的点
     */
    private void createPoint() {
        /**
         *   生成底部相当于页面个数的点
         */
        for (int i=0;i<data.size();i++){
            View point = new View(this);
            point.setBackgroundResource(R.drawable.drawable_welcome_point_blue1);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(20,20);
            if (i>0){
                params.leftMargin=(30+30)*i;
            }
            point.setLayoutParams(params);
            relativeLayout.addView(point);
        }
        pointBlack = new View(GuideActivity.this);
        pointBlack.setBackgroundResource(R.drawable.drawable_welcom_point_blue2);
        RelativeLayout.LayoutParams paramsB = new RelativeLayout.LayoutParams(20,20);
        pointBlack.setLayoutParams(paramsB);
        relativeLayout.addView(pointBlack);
    }

    protected void initAdapt() {
        viewPager.setAdapter(new MyPagerAdapt());
    }

    private class MyPagerAdapt extends PagerAdapter{

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public View instantiateItem(ViewGroup container, int position) {
            container.addView(views.get(position));
            return views.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(views.get(position));
        }
    }


}
